package dao.article;

import domain.Article;
import sun.security.pkcs11.Secmod;
import util.DButils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ArticleDaoImpl implements ArticleDao {
    /**
     * 新增文章
     * @param type
     * @param title
     * @param info
     * @param abs
     * @param change_time
     * @param author
     * @param state
     * @return int
     * @author 夏添
     * @date 2020/9/17 17:28
     */
    public int creatArticle(int type,String title,String info,String abs,String change_time,String author,int state) throws Exception{
        String sql = "insert into article (type,title,info,abs,change_time,author,state) values ("
                +type+",'"+title+"','"+info+"','"+abs+"','"+change_time+"','"+author+"',"+state+")";
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        int i = st.executeUpdate(sql);
        DButils.close(st,con);
        return i;
    }

    /**
     * 后台获取文章列表
     * @return List<Article>
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public List<Article> getArticleList() throws Exception{
        String sql = "select * from Article order by change_time desc";
        List<Article> list = new ArrayList<>();
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            Article article = new Article();
            article.setId(rs.getInt("id"));
            article.setTitle(rs.getString("title"));
            article.setState(rs.getInt("state"));
            article.setChangeTime(rs.getString("change_time"));
            article.setType(rs.getInt("type"));
            list.add(article);
        }
        DButils.close(rs,st,con);
        return list;
    }

    /**
     * 获取文章详情
     * @param id
     * @return Artlcle
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public Article getArticleInfo(int id) throws Exception{
        String sql = "select * from Article where id="+id;
        Article article = new Article();
        Connection con = DButils.getConn();
        Statement st =con.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            article.setId(rs.getInt("id"));
            article.setTitle(rs.getString("title"));
            article.setInfo(rs.getString("info"));
            article.setAbs(rs.getString("abs"));
            article.setChangeTime(rs.getString("change_time"));
            article.setState(rs.getInt("state"));
            article.setAuthor(rs.getString("author"));
            article.setType(rs.getInt("type"));
        }
        DButils.close(rs,st,con);
        return article;
    }

    /**
     * 前台获取文章列表
     * @param type
     * @return List<Article>
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public List<Article> getNewsList(int type) throws Exception{
        String sql = "select * from Article  where state=1 and type="+type+" order by change_time desc";
        List<Article> list = new ArrayList<>();
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while (rs.next()){
            Article article = new Article();
            article.setId(rs.getInt("id"));
            article.setTitle(rs.getString("title"));
            article.setState(rs.getInt("state"));
            article.setChangeTime(rs.getString("change_time"));
            article.setAbs(rs.getString("abs"));
            list.add(article);
        }
        DButils.close(rs,st,con);
        return list;
    }

    /**
     * 编辑文章
     * @param id
     * @param type
     * @param title
     * @param info
     * @param abs
     * @param change_time
     * @param author
     * @param state
     * @return int
     * @author 夏添
     * @date 2020/9/17 17:28
     */
    public int changeArticle(int id,int type,String title,String info,String abs,String change_time,String author,int state) throws Exception{
        String sql="update Article set type="+type+",title='"+title+"',info='"+info+"',abs='"+abs+"',change_time='"+change_time+"',author='"+author+"',state="+state+" where id="+id;
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        int i =st.executeUpdate(sql);
        DButils.close(st,con);
        return i;
    }

    /**
     * 删除文章
     * @param id
     * @return int
     * @author 夏添
     * @date 2020/9/24 21:03
     */
    public int DeleteArticle(int id) throws Exception{
        String sql = "delete from Article where id="+id;
        Connection con = DButils.getConn();
        Statement st = con.createStatement();
        int i = st.executeUpdate(sql);
        DButils.close(st,con);
        return i;
    }
}
